package org.jboss.tools.examples.data;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.jboss.tools.examples.model.AgendaDettaglio;
import org.jboss.tools.examples.model.Calendario;


@RequestScoped
public class AgendaDettaglioListProducer {
	@Inject
	private EntityManager em;

	private List<AgendaDettaglio> agendaDettaglio;
	
	@Produces
	@Named
	public List<AgendaDettaglio> getAgendaDettaglio() {
		return agendaDettaglio;
	}
	
	
	public void retrieveAllCalendariosOrderedByName() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Calendario> criteria = cb.createQuery(Calendario.class);
		Root<Calendario> member = criteria.from(Calendario.class);
		// Swap criteria statements if you would like to try out type-safe criteria queries, a new
		// feature in JPA 2.0
		// criteria.select(member).orderBy(cb.asc(member.get(Calendario_.name)));
		criteria.select(member).orderBy(cb.asc(member.get("data")));
//		appuntamenti = em.createQuery(criteria).getResultList();
	}
	
	@PostConstruct
	public List<AgendaDettaglio> getAgendaDettaglio(Date dataora) {
		Query query = em.createNamedQuery("findDettaglioByDataAndTiAge",AgendaDettaglio.class);
		query.setParameter("dataora", dataora, TemporalType.DATE);

		agendaDettaglio = query.getResultList();
		return agendaDettaglio;
	}

	//   public void onMemberListChanged(@Observes(notifyObserver = Reception.IF_EXISTS) final Member member) {
	//      retrieveAllMembersOrderedByName();
	//   }

	//   @PostConstruct
	//   public void retrieveAllMembersOrderedByName() {
	//	   Query query = em.createNamedQuery("findDettaglioByDataAndTiAge");
	//	   query.setParameter("dataora", dateOfBirth, TemporalType.DATE);
	//
	//      agendaDettaglio = em.createQuery(criteria).getResultList();
	//   }
}
